<?php 
header("content-type:text/html;charset=utf8");

orderGenerate($_GET);

//订单生成
function orderGenerate($data){
	$pdo = new PDO('mysql:host=123.57.16.97;dbname=project','root','zqVnUy6cxsQXbpEZ');
	$pdo->exec("set names utf8");
	$order = StrOrderOne();
	$time = time();
	$desc = '购买'.$data['movieName'];
	$uid = $data['uid'];
	$mid = $data['mid'];
	$price = $data['price'];
	$sql = "INSERT INTO `order`(uid,mid,`desc`,`order`,`status`,begin_time,end_time,price) VALUES('$uid','$mid','$desc','$order','0','$time','$time','$price')";
	$res = $pdo->exec($sql);
	if ($res) {
		//进行支付
		wxPay($order,$data['openid'],$price,$desc);
	}else{
		//订单生成错误
		header("location:https://pay.hispot.cn/app/h5/error.html");
	}
}

function wxPay($order,$openid,$price,$desc){
	include("./wxPay.php");
	$obj = new wxPay();
	$totalprice = $price*100; //支付金额
	$obj->SetBody($desc);	  //设置body
	$obj->SetOut_trade_no($order); //设置订单号
	$obj->SetTotal_fee($totalprice);	  //设置支付金额
	$obj->SetNotify_url("https://pay.hispot.cn/app/wxPayCallback.php");	//设置回调地址
	$obj->SetTrade_type('JSAPI');
	$obj->SetOpenid($openid);
	$order = $obj->unifiedOrder();
	// echo "<pre>";
	// var_dump($order);
	// exit;
	$jsApiParameters = $obj->GetJsApiParameters($order);
	$url = "https://pay.hispot.cn/app/h5/jsapi.php?data=".$jsApiParameters;
	header('location:'.$url);
}

//生成订单号
function StrOrderOne(){
    return date('ymd').substr(implode(NULL, array_map('ord', str_split(substr(uniqid(), 7, 13), 1))), 0, 8);
}


 ?>
